package com.peternguyen.totolistpro;

import java.io.ByteArrayOutputStream;

import com.peternguyen.totolistpro.dao.CategoryDAO;
import com.peternguyen.totolistpro.data.Category;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

public class AddCategory extends Activity implements OnClickListener {
	ImageButton btnAddImg;
	CategoryDAO dao;
	EditText txtName;
	EditText txtShareEmail;
	EditText txtDisc;
	ImageButton imgData;
	ImageButton btnAdd;
	ImageButton btnBack;
	byte[] img = null;
	Category cat;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addcat);

		btnAddImg = (ImageButton) findViewById(R.id.btnAddImgCat);
		btnAddImg.setOnClickListener(this);

		btnAdd = (ImageButton) findViewById(R.id.btnAddcat);
		btnBack = (ImageButton) findViewById(R.id.btn_todo_back);
		
		btnAdd.setOnClickListener(this);
		btnBack.setOnClickListener(this);

		txtName = (EditText) findViewById(R.id.txtNameCat);

		//imgData = (ImageButton)findViewById(R.id.btnAddImgCat);

		// Lay thong tin
		cat = (Category) getIntent().getSerializableExtra(
				CategoryActivity.CATEGORY);
		if (cat != null) {
			txtName.setText(cat.get_name());
			try {
				img = cat.get_img();
				Bitmap bitmap = BitmapFactory.decodeByteArray(img, 0,
						img.length);
				bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.facebook);
				btnAddImg.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 120,
						120, false));
			} catch (Exception e) {
			}
		}
	}

	@Override
	public void onClick(View v) {
		int key = v.getId();
		switch (key) {
		case R.id.btnAddImgCat:
			Intent pickPhoto = new Intent(
					Intent.ACTION_PICK,
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
			startActivityForResult(pickPhoto, 1);// one can be replced with any
													// action code
			break;
		case R.id.btnAddcat:
			dao = new CategoryDAO(AddCategory.this);
			if (cat != null) {
				cat.set_name(txtName.getText().toString());
				cat.set_img(img);
				dao.UpdatePWD(cat);
			}else{
				cat = new Category();
				cat.set_name(txtName.getText().toString());
				cat.set_img(img);
				dao.createCat(cat);
			}
			Intent i = new Intent(AddCategory.this, CategoryActivity.class);
			startActivity(i);
			AddCategory.this.finish();
			break;
		
		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Uri _uri = data.getData();

			// User had pick an image.
			Cursor cursor = getContentResolver()
					.query(_uri,
							new String[] { android.provider.MediaStore.Images.ImageColumns.DATA },
							null, null, null);
			cursor.moveToFirst();

			// Link to the image
			final String imageFilePath = cursor.getString(0);
			Bitmap bm = BitmapFactory.decodeFile(imageFilePath);
			//bm = BitmapFactory.decodeResource(getResources(),R.drawable.backward);
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			bm.compress(CompressFormat.JPEG, 100, stream);
			img = stream.toByteArray();
			
			btnAddImg.setImageBitmap(Bitmap.createScaledBitmap(bm, 120, 120,
					false));
			cursor.close();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
